package Compare;

import java.io.*;

/**
 * @Program: CompareFiles
 * @Description:
 * @Author: zqs
 * @Create： 2021-07-14-15:34
 */
public class IOTranslation {
    public static void translation(File inFile,File outFile) {
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fis = new FileInputStream(inFile);
            fos = new FileOutputStream(outFile);

            //最多一次拷贝1M
            byte[] bytes = new byte[1024 * 1024];
            //一边读一边写,每次读1M，循环直至末尾反馈为 -1 ，当读至末尾时，返回-1 ，如果不用循环，一次就只读数组的大小
            int readCount = 0;
            while ((readCount = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, readCount);
            }
            //刷新，输出流最后一定要刷新
            fos.flush();

            /*如果不一边读一边写，而是全部读完，全部写，就定义数组大小为fis.available();
            byte [] bytes = new byte[fis.available();];
            fos.write(fis.read(bytes)); //如果实现不了就拆出来
            */
        } catch (
                FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
